<?php
// PANDA - RECENT POSTS WIDGET v.2.0
// www.pandathemes.com
add_action('init', 'widget_recent_posts_multi_register');
function widget_recent_posts_multi_register() {
	
	$prefix = 'recent-posts-multi'; // $id prefix
	$name = __('Panda - Recent Posts v.2.0');
	$widget_ops = array('classname' => 'widget_recent_posts_multi', 'description' => __('Customizable multiple widget.'));
	$control_ops = array('width' => 200, 'height' => 200, 'id_base' => $prefix);
	
	$options = get_option('widget_recent_posts_multi');
	if(isset($options[0])) unset($options[0]);
	
	if(!empty($options)){
		foreach(array_keys($options) as $widget_number){
			wp_register_sidebar_widget($prefix.'-'.$widget_number, $name, 'widget_recent_posts_multi', $widget_ops, array( 'number' => $widget_number ));
			wp_register_widget_control($prefix.'-'.$widget_number, $name, 'widget_recent_posts_multi_control', $control_ops, array( 'number' => $widget_number ));

		}
	} else{
		$options = array();
		$widget_number = 1;
		wp_register_sidebar_widget($prefix.'-'.$widget_number, $name, 'widget_recent_posts_multi', $widget_ops, array( 'number' => $widget_number ));
		wp_register_widget_control($prefix.'-'.$widget_number, $name, 'widget_recent_posts_multi_control', $control_ops, array( 'number' => $widget_number ));
	}
}

function widget_recent_posts_multi($args, $vars = array()) {
    extract($args);
    $widget_number = (int)str_replace('recent-posts-multi-', '', @$widget_id);
    $options = get_option('widget_recent_posts_multi');
    if(!empty($options[$widget_number])){
    	$vars = $options[$widget_number];
    }
	$before_title = "<h3>";
	$after_title = "</h3>";
	$widget_title = $vars['widget_title'];
	$cats = $vars['cats'];
	$quantity = $vars['quantity'];
	$exclude = $vars['exclude'];
	$order = $vars['order'];
	$thumbs = $vars['thumbs'];
	$thumbs_float = $vars['thumbs_float'];
	$width = $vars['width'];
	$height = $vars['height'];
	$titles = $vars['titles'];
	$size = $vars['size'];
	$date = $vars['date'];
	$excerpt = $vars['excerpt'];
    // DISPALY WIDGET
	echo $before_widget;
	?>
		<?php if(!empty($vars['widget_title'])){ echo $before_title . $widget_title . $after_title; } ?>
		<ul id="widget_posts" class="float_<?php echo $thumbs_float ?>">
		<?php
			$q = $quantity + $exclude;
			$i = 0;
		query_posts("showposts=$q&cat=$cats&$order");
		while (have_posts()) : the_post();
		if ($i < $exclude) { $i++ ;}
		else { ?>
			<li>
				<?php
				if ($thumbs=="display") {
					$values = get_post_custom_values("full_image_value");
					$image2 = $values[0];
					// CHECK IMAGE POST
					if ($image2) { ?><a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo $image2 ?>&amp;h=<?php echo $height ?>&amp;w=<?php echo $width ?>&amp;zc=1&amp;q=90" width="<?php echo $width ?>" height="<?php echo $height ?>" alt="<?php the_title(); ?>" /></a><?php }
					// CHECK AUTOMATIC THUMB
					elseif (catch_that_image()<>"") { ?><a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo catch_that_image() ?>&amp;h=<?php echo $height ?>&amp;w=<?php echo $width ?>&amp;zc=1&amp;q=90" width="<?php echo $width ?>" height="<?php echo $height ?>" alt="<?php the_title(); ?>" /></a><?php }
					// ELSE DEFAULT
					else { ?><a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_directory'); ?>/images/default.gif" width="<?php echo $width ?>" height="<?php echo $height ?>" alt="no image"/></a><?php ;}
				;}?>
				<?php // SELECT SIZE OF TITLE
					if ($titles=='display') {
						if ( ($size=="default") || (!($size<>"")) ) { ?><div class="f14 wt"><a href="<?php the_permalink() ?>"><?php the_title() ?></a></div><?php }
						elseif ($size > 17) { ?><h5 class="f<?php echo $size ?>"><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h5><?php }
						else { ?><div class="f<?php echo $size ?> wt"><a href="<?php the_permalink() ?>"><?php the_title() ?></a></div><?php }
					}
				?>
				<?php if ($date=='display') { ?>
					<span><?php the_time('M j, Y') ?></span>
				<?php ;} ?>
				<?php if ($excerpt=='display') { 
					the_excerpt()
				;} ?>
				<div class="clear"><!-- --></div>
			</li>
		<?php ;} endwhile;
		?>
		</ul>
		<div class="clear"><!-- --></div>
	<?php
   	echo $after_widget;
}

function widget_recent_posts_multi_control($args) {

	$prefix = 'recent-posts-multi'; // $id prefix
	
	$options = get_option('widget_recent_posts_multi');
	if(empty($options)) $options = array();
	if(isset($options[0])) unset($options[0]);
		
	// update options array
	if(!empty($_POST[$prefix]) && is_array($_POST)){
		foreach($_POST[$prefix] as $widget_number => $values){
			if(empty($values) && isset($options[$widget_number])) // user clicked cancel
				continue;
			
			if(!isset($options[$widget_number]) && $args['number'] == -1){
				$args['number'] = $widget_number;
				$options['last_number'] = $widget_number;
			}
			$options[$widget_number] = $values;
		}
		
		// update number
		if($args['number'] == -1 && !empty($options['last_number'])){
			$args['number'] = $options['last_number'];
		}

		// clear unused options and update options in DB. return actual options array
		$options = bf_smart_multiwidget_update($prefix, $options, $_POST[$prefix], $_POST['sidebar'], 'widget_recent_posts_multi');
	}
	$number = ($args['number'] == -1)? '%i%' : $args['number'];

	// WIDGET VARS
	$opts = @$options[$number];
	$title = @$opts['title'];
	$widget_title = @$opts['widget_title'];
	$cats = @$opts['cats'];
	$quantity = @$opts['quantity'];
	$exclude = @$opts['exclude'];
	$order = @$opts['order'];
	$thumbs = @$opts['thumbs'];
	$thumbs_float = @$opts['thumbs_float'];
	$width = @$opts['width'];
	$height = @$opts['height'];
	$titles = @$opts['titles'];
	$size = @$opts['size'];
	$date = @$opts['date'];
	$excerpt = @$opts['excerpt'];
	// WIDGET CONTROLS
	?>
	<input style="display:none;" type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][title]" value="<?php echo $title; ?>" />
	<p>
		Widget title:
		<input class="widefat" type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][widget_title]" value="<?php echo $widget_title; ?>" />
	</p>
	<p>
		Enter ID of categories e.g. 1,2,3,4
		<input class="widefat" type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][cats]" value="<?php echo $cats; ?>" />
	</p>
	<p>
		Posts:
		<select name="<?php echo $prefix; ?>[<?php echo $number; ?>][quantity]">
			<option value="1" <?php if ($quantity=="1"):?> selected <?php endif; ?>>1</option>
			<option value="2" <?php if ($quantity=="2"):?> selected <?php endif; ?>>2</option>
			<option value="3" <?php if ($quantity=="3"):?> selected <?php endif; ?>>3</option>
			<option value="4" <?php if ($quantity=="4"):?> selected <?php endif; ?>>4</option>
			<option value="5" <?php if ($quantity=="5"):?> selected <?php endif; ?>>5</option>
			<option value="6" <?php if ($quantity=="6"):?> selected <?php endif; ?>>6</option>
			<option value="7" <?php if ($quantity=="7"):?> selected <?php endif; ?>>7</option>
			<option value="8" <?php if ($quantity=="8"):?> selected <?php endif; ?>>8</option>
			<option value="9" <?php if ($quantity=="9"):?> selected <?php endif; ?>>9</option>
			<option value="10" <?php if ($quantity=="10"):?> selected <?php endif; ?>>10</option>
			<option value="11" <?php if ($quantity=="11"):?> selected <?php endif; ?>>11</option>
			<option value="12" <?php if ($quantity=="12"):?> selected <?php endif; ?>>12</option>
			<option value="13" <?php if ($quantity=="13"):?> selected <?php endif; ?>>13</option>
			<option value="14" <?php if ($quantity=="14"):?> selected <?php endif; ?>>14</option>
			<option value="15" <?php if ($quantity=="15"):?> selected <?php endif; ?>>15</option>
		</select>
		Exclude:
		<select name="<?php echo $prefix; ?>[<?php echo $number; ?>][exclude]">
			<option value="0" <?php if ($exclude=="0"):?> selected <?php endif; ?>>none</option>
			<option value="1" <?php if ($exclude=="1"):?> selected <?php endif; ?>>1 last</option>
			<option value="2" <?php if ($exclude=="2"):?> selected <?php endif; ?>>2 last</option>
			<option value="3" <?php if ($exclude=="3"):?> selected <?php endif; ?>>3 last</option>
			<option value="4" <?php if ($exclude=="4"):?> selected <?php endif; ?>>4 last</option>
			<option value="5" <?php if ($exclude=="5"):?> selected <?php endif; ?>>5 last</option>
			<option value="6" <?php if ($exclude=="6"):?> selected <?php endif; ?>>6 last</option>
			<option value="7" <?php if ($exclude=="7"):?> selected <?php endif; ?>>7 last</option>
			<option value="8" <?php if ($exclude=="8"):?> selected <?php endif; ?>>8 last</option>
			<option value="9" <?php if ($exclude=="9"):?> selected <?php endif; ?>>9 last</option>
			<option value="10" <?php if ($exclude=="10"):?> selected <?php endif; ?>>10 last</option>
			<option value="11" <?php if ($exclude=="11"):?> selected <?php endif; ?>>11 last</option>
			<option value="12" <?php if ($exclude=="12"):?> selected <?php endif; ?>>12 last</option>
			<option value="13" <?php if ($exclude=="13"):?> selected <?php endif; ?>>13 last</option>
			<option value="14" <?php if ($exclude=="14"):?> selected <?php endif; ?>>14 last</option>
			<option value="15" <?php if ($exclude=="15"):?> selected <?php endif; ?>>15 last</option>
		</select>
	</p>
	<p>
		Order posts by:
		<select name="<?php echo $prefix; ?>[<?php echo $number; ?>][order]">
			<option value="" <?php if ($order==""):?> selected <?php endif; ?>>date</option>
			<option value="orderby=rand" <?php if ($order=="orderby=rand"):?> selected <?php endif; ?>>random</option>
		</select>
		<div style="height:12px; border-top:2px solid #DFDFDF;"><!-- --></div>
	</p>
	<p>
		Thumbs:
		<select name="<?php echo $prefix; ?>[<?php echo $number; ?>][thumbs]">
			<option value="hide" <?php if ($thumbs=="hide"):?> selected <?php endif; ?>>hide</option>
			<option value="display" <?php if ($thumbs=="display"):?> selected <?php endif; ?>>display</option>
		</select>
		<select name="<?php echo $prefix; ?>[<?php echo $number; ?>][thumbs_float]">
			<option value="left" <?php if ($thumbs_float=="left"):?> selected <?php endif; ?>>Float: left</option>
			<option value="right" <?php if ($thumbs_float=="right"):?> selected <?php endif; ?>>Float: right</option>
			<option value="none" <?php if ($thumbs_float=="none"):?> selected <?php endif; ?>>Float: none</option>
		</select>
	</p>
	<p>
		Width:<input type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][width]" size="3" value="<?php if ($width<>"") { echo $width ;} else { echo '70' ;} ?>" />
		Height:<input type="text" name="<?php echo $prefix; ?>[<?php echo $number; ?>][height]" size="3" value="<?php if ($height<>"") { echo $height ;} else { echo '50' ;} ?>" /> px
		<div style="height:12px; border-top:2px solid #DFDFDF;"><!-- --></div>
	</p>
	<p>
		Titles:
		<select name="<?php echo $prefix; ?>[<?php echo $number; ?>][titles]">
			<option value="display" <?php if ($titles=="display"):?> selected <?php endif; ?>>display</option>
			<option value="hide" <?php if ($titles=="hide"):?> selected <?php endif; ?>>hide</option>
		</select>
		Size:
		<select name="<?php echo $prefix; ?>[<?php echo $number; ?>][size]">
			<option value="default" <?php if ($size=="default"):?> selected <?php endif; ?>>default</option>
			<option value="12" <?php if ($size=="12"):?> selected <?php endif; ?>>12 px</option>
			<option value="13" <?php if ($size=="13"):?> selected <?php endif; ?>>13 px</option>
			<option value="14" <?php if ($size=="14"):?> selected <?php endif; ?>>14 px</option>
			<option value="15" <?php if ($size=="15"):?> selected <?php endif; ?>>15 px</option>
			<option value="16" <?php if ($size=="16"):?> selected <?php endif; ?>>16 px</option>
			<option value="18" <?php if ($size=="18"):?> selected <?php endif; ?>>18 px</option>
			<option value="20" <?php if ($size=="20"):?> selected <?php endif; ?>>20 px</option>
			<option value="22" <?php if ($size=="22"):?> selected <?php endif; ?>>22 px</option>
			<option value="24" <?php if ($size=="24"):?> selected <?php endif; ?>>24 px</option>
			<option value="26" <?php if ($size=="26"):?> selected <?php endif; ?>>26 px</option>
			<option value="30" <?php if ($size=="30"):?> selected <?php endif; ?>>30 px</option>
		</select>
	</p>
	<p>
		Date:
		<select name="<?php echo $prefix; ?>[<?php echo $number; ?>][date]">
			<option value="hide" <?php if ($date=="hide"):?> selected <?php endif; ?>>hide</option>
			<option value="display" <?php if ($date=="display"):?> selected <?php endif; ?>>display</option>
		</select>
	</p>
	<p>
		Excerpt:
		<select name="<?php echo $prefix; ?>[<?php echo $number; ?>][excerpt]">
			<option value="hide" <?php if ($excerpt=="hide"):?> selected <?php endif; ?>>hide</option>
			<option value="display" <?php if ($excerpt=="display"):?> selected <?php endif; ?>>display</option>
		</select>
	</p>
	<?php
}

// helper function can be defined in another plugin
if(!function_exists('bf_smart_multiwidget_update')){
	function bf_smart_multiwidget_update($id_prefix, $options, $post, $sidebar, $option_name = ''){
		global $wp_registered_widgets;
		static $updated = false;

		// get active sidebar
		$sidebars_widgets = wp_get_sidebars_widgets();
		if ( isset($sidebars_widgets[$sidebar]) )
			$this_sidebar =& $sidebars_widgets[$sidebar];
		else
			$this_sidebar = array();
		
		// search unused options
		foreach ( $this_sidebar as $_widget_id ) {
			if(preg_match('/'.$id_prefix.'-([0-9]+)/i', $_widget_id, $match)){
				$widget_number = $match[1];
				
				// $_POST['widget-id'] contain current widgets set for current sidebar
				// $this_sidebar is not updated yet, so we can determine which was deleted
				if(!in_array($match[0], $_POST['widget-id'])){
					unset($options[$widget_number]);
				}
			}
		}
		
		// update database
		if(!empty($option_name)){
			update_option($option_name, $options);
			$updated = true;
		}
		
		// return updated array
		return $options;
	}
}
?>